<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button v-permission="'parking:rule:add_edit'" type="primary" @click="addChargeRule">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ formatChargeType(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="">
            <el-button v-permission="'parking:rule:add_edit'" size="mini" type="text">编辑</el-button>
            <el-button v-permission="'parking:rule:remove'" size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-container">
        <el-pagination
          layout="total, prev, pager, next"
          :page-size="params.pageSize"
          :total="total"
          @current-change="handlePageChange"
        />
      </div>
    </div>
    <AddRule :dialog-visible.sync="dialogVisible" />
  </div>
</template>

<script>
import { utils, writeFileXLSX } from 'xlsx'
import { ruleList } from '@/api/carRule'
import AddRule from './components/addRule.vue'
export default {
  name: 'Building',
  components: { AddRule },
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 5
      },
      total: 0,
      dialogVisible: false // 控制弹框关闭打开
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    // 获取计费规则列表
    async getRuleList() {
      const { data: res } = await ruleList(this.params)
      this.ruleList = res.rows
      this.total = res.total
    },
    // 页码变化
    handlePageChange(val) {
      this.params.page = val
      this.getRuleList()
    },
    // 计算序号
    indexMethod(index) {
      // 计算序号
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 导出excel
    async exportExcel() {
      // 获取要导出的业务数据
      const { data: res } = await ruleList(this.params)
      // 表头英文字段key
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration']
      // 表头中文字段value
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)']
      const list = res.rows.map(item => {
        const obj = {}
        tableHeaderKeys.forEach(key => {
          obj[key] = item[key]
        })
        return obj
      })
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(list)

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    },
    // 格式化计费方式
    formatChargeType(charge) {
      const chargeMap = {
        'duration': '按时长收费',
        'turn': '按次收费',
        'partition': '分段收费'
      }
      return chargeMap[charge]
    },
    addChargeRule() {
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
